package com.duowei.diandan.fragment.dialog;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;

import com.duowei.diandan.R;
import com.duowei.diandan.adapter.TableTypeAdapter;
import com.duowei.diandan.databinding.OpenTableDialogFragmentBinding;
import com.duowei.diandan.db.entry.Gklx;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.List;
import java.util.Locale;

/**
 * 下单模式(开单窗口)
 */
public class OpenTableDialogFragment extends BaseDialogFragment {

    private OpenTableDialogFragmentBinding mBinding;


    private TableTypeAdapter mTableTypeAdapter;

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
//            window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
            //noinspection ConstantConditions
            window.setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.dialog_bg));
            window.setDimAmount(0.25f);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_open_table_dialog, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //
        List<Gklx> gklxList = getDb().gklxDao().findAll();
        if (gklxList.size() == 0) {
            mBinding.tableTypeLayout.setVisibility(View.GONE);
        } else {
            mBinding.tableTypeLayout.setVisibility(View.VISIBLE);

            FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(getContext(), FlexDirection.ROW, FlexWrap.WRAP);
            mBinding.tableTypeRecyclerView.setLayoutManager(layoutManager);
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            mTableTypeAdapter = new TableTypeAdapter(gklxList);
            mBinding.tableTypeRecyclerView.setAdapter(mTableTypeAdapter);

            //
            if (!getResources().getBoolean(R.bool.is_table)) {
                mBinding.contentLayout.setOrientation(LinearLayout.VERTICAL);

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                mBinding.tableTypeLayout.setLayoutParams(params);
                mBinding.peopleLayout.setLayoutParams(params);
            }
        }

        //
        mBinding.minusButton.setOnClickListener(v -> {
            int peopleNum = getPeopleNum();
            if (peopleNum > 1) {
                setPeopleNum(peopleNum - 1);
            }
        });
        mBinding.plusButton.setOnClickListener(v -> {
            int peopleNum = getPeopleNum();
            setPeopleNum(peopleNum + 1);
        });


        mBinding.cancelButton.setOnClickListener(v -> dismiss());
        mBinding.confirmButton.setOnClickListener(v -> {
            if (mOnConfirmClickListener != null) {
                Gklx gklx = null;
                if (mTableTypeAdapter != null) {
                    gklx = mTableTypeAdapter.getCheckedItem();
                }

                mOnConfirmClickListener.onConfirmClick(gklx, getPeopleNum());
            }


            //
            dismiss();
        });
    }

    private int getPeopleNum() {
        return Integer.valueOf(mBinding.peopleView.getText().toString());
    }

    private void setPeopleNum(int i) {
        mBinding.peopleView.setText(String.format(Locale.CHINA, "%02d", i));
    }

    private OnConfirmClickListener mOnConfirmClickListener;

    public void setOnConfirmClickListener(OnConfirmClickListener onConfirmClickListener) {
        mOnConfirmClickListener = onConfirmClickListener;
    }

    public interface OnConfirmClickListener {
        void onConfirmClick(Gklx gklx, int peopleNum);
    }
}
